﻿using PLCS.App.Shared.ViewModels;
using PLCS.Application.Contracts.LogShowApp.Dtos;
using PLCS.Application.Contracts.StationApp.Dtos;
using PLCS.Application.Contracts.TaskApp.Dtos;
using PLCS.Client.IService.LogManager;
using PLCS.Client.IService.OrderTask;
using PLCS.Domain.Shared.Enums;
using PLCS.Domain.Shared.Tasks;
using PLCS.Shared.Common.Constant;

namespace PLCS.App.ViewModels.Dialogs;

internal class StationDetailViewModel : HostDialogViewModel
{
    private StationDto model;

    public StationDto Model
    {
        get { return model; }
        set { SetProperty(ref model, value); }
    }

    private string _endPos, _hintMessage, _trayNo;
    private TrayType _trayType;
    private TaskType _taskType = TaskType.普通流转;
    private int _level;
    private readonly ITaskService _taskService;
    private readonly IOperationLogService _logShowService;

    public string EndPos
    {
        get { return _endPos; }
        set { SetProperty(ref _endPos, value); }
    }

    public string HintMessage
    {
        get { return _hintMessage; }
        set { SetProperty(ref _hintMessage, value); }
    }

    public string TrayNo
    {
        get { return _trayNo; }
        set { SetProperty(ref _trayNo, value); }
    }

    public int Level
    {
        get { return _level; }
        set { SetProperty(ref _level, value); }
    }

    public TrayType TrayType
    {
        get { return _trayType; }
        set { SetProperty(ref _trayType, value); }
    }

    public TaskType TaskType
    {
        get { return _taskType; }
        set { SetProperty(ref _taskType, value); }
    }

    public DelegateCommand SendTaskCommand { get; }

    public override void OnDialogOpened(IDialogParameters parameters)
    {
        if (parameters.ContainsKey("Value"))
        {
            Model = parameters.GetValue<StationDto>("Value");
            Title = "编辑";
        }
        else
        {
            Title = "新增";
            Model = new StationDto();
        }
    }

    public StationDetailViewModel(ITaskService taskService, IOperationLogService logShowService)
    {
        View = "工位详情弹窗";
        SendTaskCommand = new DelegateCommand(SendTask);
        this._taskService = taskService;
        this._logShowService = logShowService;
    }

    private async void SendTask()
    {
        try
        {
            SavebuttonIsEnabled = false;
            if (string.IsNullOrEmpty(EndPos) && TaskType == TaskType.普通流转)
            {
                HintMessage = "目标位不可为空";
                return;
            }
            var task = new TaskEditDto()
            {
                EndPos = EndPos,
                TrayNo = TrayNo,
                Level = Level,
                StartPos = Model.StaNum,
                TaskType = TaskType,
                TrayType = TrayType,
            };
            await _taskService.CreateAsync(task);
            HintMessage = "下发成功";
            Log("下发任务", task);
        }
        catch (Exception ex)
        {
            HintMessage = ex.Message;
        }
        finally
        {
            SavebuttonIsEnabled = true;
        }
    }
}